package com.seaboxdata.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.seaboxdata.entity.ElectronicCoupletUploadPicture;
import com.seaboxdata.entity.ElectronicCoupletUploadPictureVo;
import com.seaboxdata.service.ElectronicCoupletUploadPictureService;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2021-07-21 15:05
 **/
@RestController
@EnableAutoConfiguration
@RequestMapping("/api/ElectronicCouplet")
public class ElectronicCoupletUploadPictureController {

    @Value("${default.md5.key}")
    private String key;

    @Autowired
    private ElectronicCoupletUploadPictureService electronicCoupletUploadPictureService;

    @RequestMapping("/viu")
    public Map<String, Object> uploadPicture(@RequestBody ElectronicCoupletUploadPictureVo electronicCoupletUploadPictureVo) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", "00000");
        resultMap.put("message", "成功");
        resultMap.put("content", null);
        String md5Info = electronicCoupletUploadPictureVo.getMd5Info();
        ElectronicCoupletUploadPicture baseInfo = electronicCoupletUploadPictureVo.getBaseInfo();
        baseInfo.setCjsj(new Date());

        //增加校验 RecordId + ImgType + ImgName
        String md5Check = baseInfo.getRecordId() + baseInfo.getImgType() + baseInfo.getImgName() + key;
        if (StringUtils.isEmpty(md5Info) || !md5Info.equals(MD5Encoder.encode(md5Check.getBytes()))) {
            resultMap.put("code", "00004");
            resultMap.put("message", "请求信息被篡改");
            return resultMap;
        }

        try {
            electronicCoupletUploadPictureService.save(baseInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", "00001");
            resultMap.put("message", "入库失败");
        }
        return resultMap;
    }

}
